﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;

using SimpleText.Framework;
using Yogesh.ExcelXml;


public partial class ExportPage : SimpleTextPage
{
	
	protected void Page_Load(object sender, EventArgs e)
	{
		InitPage();

		if (CurrentModel == null)
		{
			Response.Redirect("projects.aspx?msg=noprojectselected");
		}
		if (!IsPostBack)
		{
		}
	}

	protected void ExportToExcelBtnClicked(object sender, EventArgs e)
	{
		Response.ContentType = "application/octet-stream";
		FileInfo fileInfo = new FileInfo(CurrentModel.LoadedFromXmlFilePath);
		Response.AddHeader("Content-disposition", "attachment; filename=" + fileInfo.Name.Replace(fileInfo.Extension, "") + "_export_excel.xml");

		ExcelXmlWorkbook book = new ExcelXmlWorkbook();
		book.Properties.Author = "SimpleText.Net";
		book.Properties.Title = "Texts from the \"" + CurrentModel.ProjectName + "\" project";
		Worksheet sheet = book[0];
		// Name is the name of the sheet. If not set, the default name (style is "sheet" + sheet number, like sheet1, sheet2)
		sheet.Name = "Texts";
		sheet.FreezeTopRows = 1;
		sheet.FreezeLeftColumns = 2;
		sheet.PrintOptions.Orientation = PageOrientation.Landscape;
		sheet.PrintOptions.SetMargins(0.5, 0.4, 0.5, 0.4);

		//header row
		sheet[0, 0].Value = "name";
		sheet[0, 0].Font.Bold = true;
		sheet.Columns(1).Width = 150; //name
		sheet.Columns(1).Style.DisplayFormat = DisplayFormatType.Text;

		int headerColumnIndex = 1;
		foreach (Language language in CurrentModel.Languages)
		{
			sheet[headerColumnIndex, 0].Value = language.ShortName;
			sheet[headerColumnIndex, 0].Font.Bold = true;
			sheet.Columns(headerColumnIndex).Width = 200; //text
			sheet.Columns(headerColumnIndex).Style.DisplayFormat = DisplayFormatType.Text;
			headerColumnIndex++;
		}
		sheet[headerColumnIndex, 0].Value = "translatorComment";
		sheet[headerColumnIndex, 0].Font.Bold = true;

		sheet.Columns(headerColumnIndex).Width = 200; //translatorComment
		sheet.Columns(headerColumnIndex).Style.DisplayFormat = DisplayFormatType.Text;


		int rowIndex = 1; //start at 1 to skip header row

		//dummy row to ensure correct re-import
		sheet[0, rowIndex].Value = "dummy_do_not_translate_edit_or_delete";
		int columnIndex = 1;
		foreach (Language language in CurrentModel.Languages)
		{
			sheet[columnIndex, rowIndex].Value = GetOleDbTextDriverLengthFixDummyString();

			columnIndex++;
		}

		sheet[columnIndex, rowIndex].Value = "";

		rowIndex++;


		//actual data rows	
		foreach (TextDefinition textDef in CurrentModel.TextDefinitions.Values)
		{

			sheet[0, rowIndex].Value = textDef.Name;
			columnIndex = 1;
			foreach (Language language in CurrentModel.Languages)
			{
				string content = "";
				if (textDef.Texts.ContainsKey(language.ShortName))
				{
					content = textDef.Texts[language.ShortName].Content;
				}
				sheet[columnIndex, rowIndex].Value = content;

				columnIndex++;
			}

			if (textDef.TranslatorComment != null)
			{
				sheet[columnIndex, rowIndex].Value = textDef.TranslatorComment;
			}

			rowIndex++;
		}

		//output Excel sheet
		string fileName = CurrentModel.ProjectName + DateTime.Now.Ticks.ToString("x");
		string filePathAndFileName = Server.MapPath("~/App_Data/exports/" + fileName);
		book.Export(filePathAndFileName);

		Response.Clear();
		Response.WriteFile(filePathAndFileName);
		Response.End();
	}

	protected void ExportToXmlBtnClicked(object sender, EventArgs e)
	{
		Response.ContentType = "application/octet-stream";
		FileInfo fileInfo = new FileInfo(CurrentModel.LoadedFromXmlFilePath);
		Response.AddHeader("Content-disposition", "attachment; filename=" + fileInfo.Name.Replace(fileInfo.Extension, "") + "_export_custom.xml");
		//text definitions

		StringBuilder xmlSB = new StringBuilder("");

		xmlSB.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\r\n");
		xmlSB.Append("<textDefinitions>" + "\r\n");

		//dummy row to ensure correct re-import
		xmlSB.Append("\t" + "<textDefinition>" + "\r\n");
		xmlSB.Append("\t\t" + "<name>dummy_do_not_translate_edit_or_delete</name>" + "\r\n");
		foreach (Language language in CurrentModel.Languages)
			{
				xmlSB.Append("\t\t" + "<" + language.ShortName + ">" + "<![CDATA[" + GetOleDbTextDriverLengthFixDummyString() + "]]>" + "</" + language.ShortName + ">" + "\r\n");
			}

		xmlSB.Append("\t" + "</textDefinition>" + "\r\n");

		//the actual texts
		foreach (TextDefinition textDef in CurrentModel.TextDefinitions.Values)
		{
			xmlSB.Append("\t" + "<textDefinition>" + "\r\n");

			xmlSB.Append("\t\t" + "<name>" + textDef.Name + "</name>" + "\r\n");

			foreach (Language language in CurrentModel.Languages)
			{
				string content = "";
				if (textDef.Texts.ContainsKey(language.ShortName))
				{
					content = textDef.Texts[language.ShortName].Content;
				}
				xmlSB.Append("\t\t" + "<" + language.ShortName + ">" + "<![CDATA[" + content + "]]>" + "</" + language.ShortName + ">" + "\r\n");
			}

			xmlSB.Append("\t\t" + "<translatorComment>" + textDef.TranslatorComment + "</translatorComment>" + "\r\n");

			xmlSB.Append("\t" + "</textDefinition>" + "\r\n");
		}

		xmlSB.Append("</textDefinitions>" + "\r\n");


		Response.Clear();
		Response.Write(xmlSB.ToString());
		Response.End();
	}

	public string GetOleDbTextDriverLengthFixDummyString()
	{
		return
			"DO NOT TRANSLATE EDIT OR DELETE THIS ROW. This is a long string (exceeding 255 chars). It is meant to ensure that the MS Office OleDB drivers (Access Database Engine 2010) do not crop the fields at 255 characters. The driver samples an unspecified number of rows to determine which size to use for the individual columns (255 or larger). If all the sampled rows are smaller than 255 chars, then all values exceeding that length will be silently truncated.";
	}



}
